<?
session_start();
include('conn.php');
$opt = $_REQUEST["q"];
	switch($opt)
	{
		case '0':
			$qry1 = "SELECT * from replaces where status = '1' order by id desc;";
			$qry = mysql_query($qry1);
			$data = array();
			while($rows = mysql_fetch_array($qry))
			{
				$q1 = "select stbno from stbTable where id = '".$rows['oldSTB']."'";
				$r1 = mysql_fetch_array(mysql_query($q1));
				$q2 = "select stbno from stbTable where id = '".$rows['newSTB']."'";
				$r2 = mysql_fetch_array(mysql_query($q2));
				$q3 = "select cardno from cardTable where id = '".$rows['cardno']."'";
				$r3 = mysql_fetch_array(mysql_query($q3));
				$data[] = array
					(							
						"id" => $rows['id'],			
						"oldstb" => $r1['stbno'],
						"newstb" => $r2['stbno'],
						"card" => $r3['cardno'],
						"rdate" => $rows['replacedate'],
						"reason" => $rows['reason'],
						"comments" => $rows['comments']			
					);
			}
			echo json_encode($data);
			break;
			
		case '1':
			$oldstbno = $_REQUEST['oldstbno'];
			
			$qry1 = "SELECT cardno from cardTable where id in (select cardid from STBDetails where stbid in (select id from stbTable where stbno='$oldstbno')); ";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))			
				$data = $rows['cardno'];								
			echo $data;									
			break;
		
		case '2':
			$qry1 = "SELECT stbno from stbTable where flag='occ' order by id ";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))
			{
				$data[] = $rows['stbno']	;					
			}
			echo json_encode($data);
			break;
			
		case '3':
			$qry1 = "SELECT stbno from stbTable where flag='free' order by id ";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))
			{
				$data[] = $rows['stbno']	;					
			}
			echo json_encode($data);
			break;
	}
		
?>